
public class PerfectNumber {
	public static void main( String[] args ) {
		int max = Integer.parseInt(args[0]);

		for (int i=2; i<=max; ++i)
			if ( isPerfect(i) ) System.out.println( i + " is perfect.");
	}

	private static boolean isPerfect(int n) {
		int sum=1;
		int maxFactor = (int)(Math.sqrt(n));
		if ( maxFactor*maxFactor == n ) sum+=maxFactor;

		if ( n%2 == 0 ) {
			for (int i=2; i<maxFactor; ++i) 
				if ( n%i == 0) {
					sum+=i;
					sum += n/i;
				}
		} else {
			for (int i=3; i<maxFactor; i+=2) 
				if ( n%i == 0) {
					sum+=i;
					sum += n/i;
				}
		}

		if (sum==n) return true;
		else return false;

	}
}
